package com.cobo.fitness.core.meillisearch;

import com.alibaba.fastjson.JSON;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.SearchResult;
import com.meilisearch.sdk.model.Searchable;
import com.meilisearch.sdk.model.Task;
import com.meilisearch.sdk.model.TaskInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class MeilsearchEngine implements SearchApi{

    @Value("${meilisearch.host}")
    private String meilisearchHost;

    @Value("${meilisearch.master-key}")
    private String meilisearchMasterKey;

    private static void test1() throws Exception{
        String s = FileUtils.readFileToString(new File("D:\\code\\k-tool\\server\\kcsp-web-tool\\src\\main\\resources\\movies.json"), "utf-8");
        Client client = new Client(new Config("http://192.168.2.15:8881", "aPm4RHCzrmUlzFyYHEPAQ4H81t9bEjACquK1jPCfB3k"));
        Index index = client.index("movies");
        TaskInfo taskInfo = index.addDocuments(s);
        System.out.println(taskInfo);
    }
    private static void test2()throws Exception{
//        String s = FileUtils.readFileToString(new File("/media/cobo/0DD80F300DD80F30/code/k-tool/server/src/main/resources/movies.json"), "utf-8");
        Client client = new Client(new Config("http://192.168.2.15:8881", "aPm4RHCzrmUlzFyYHEPAQ4H81t9bEjACquK1jPCfB3k"));
        Task task = client.getTask(0);
        System.out.printf(task.getDuration());
//        test1();
//        Client client = new Client(new Config("http://192.168.2.15:8881", "aPm4RHCzrmUlzFyYHEPAQ4H81t9bEjACquK1jPCfB3k"));
//        SearchResult search = client.index("movies").search("botman");
//        System.out.println(JSON.toJSONString(search));
    }

    @Override
    public void addDocument(String indexName, List<?> docs) {
        try {
            Client client = new Client(new Config(meilisearchHost, meilisearchMasterKey));
            Index index = client.index(indexName);
            String jsonString = JSON.toJSONString(docs);
            index.addDocuments(jsonString);
        }catch (Exception e){

        }
    }

    public void updateFilter(String indexName, String[] nameArray) {
        try {
            Client client = new Client(new Config(meilisearchHost, meilisearchMasterKey));
            Index index = client.index(indexName);
            index.updateFilterableAttributesSettings(new String[]{"bodyPart"});
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void search(String index,String name) {
        try {
            Client client = new Client(new Config(meilisearchHost, meilisearchMasterKey));
            SearchResult search = client.index(index).search(name);
            ArrayList<HashMap<String, Object>> hits = search.getHits();
            int j = 1;
        } catch (Exception e) {

        }
    }

    @Override
    public Searchable search(String index, String name, int offset, int limit) {

        try {
            Client client = new Client(new Config(meilisearchHost, meilisearchMasterKey));
            SearchRequest searchRequest = SearchRequest.builder()
                    .q(name)
                    .offset(offset)
                    .limit(limit)
                    .build();
            Searchable search = client.index(index).search(searchRequest);
            return search;
        } catch (Exception e) {

        }
        return null;
    }

    @Override
    public Searchable search(String index, String name, String[]filter,int offset, int limit) {

        try {
            Client client = new Client(new Config(meilisearchHost, meilisearchMasterKey));
            SearchRequest searchRequest = SearchRequest.builder()
                    .q(name)
                    .offset(offset)
                    .limit(limit)
                    .filter(filter)
                    .build();
            Searchable search = client.index(index).search(searchRequest);
            return search;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
